package com.manage.utils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * Create date 2017/5/22
 * <p>用于查询，参数封装</p>
 *
 * @author
 * @since 1.0.0
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"}, ignoreUnknown = true)
public class Criteria {
    private Map<String, Object> condition;
    private String orders;
    private Integer offset;
    private Integer fetchSize;

    /**
     *
     */
    public Criteria() {
        this.condition = new HashMap();
    }

    /**
     *
     */
    public void clear() {
        this.condition.clear();
    }

    /**
     * @param condition
     * @param value
     * @return
     */
    public Criteria put(String condition, Object value) {
        this.condition.put(condition, value);
        return this;
    }

    /**
     * @param key
     * @return
     */
    public Object get(String key) {
        return this.condition.get(key);
    }

    /**
     * @param key
     * @return
     */
    public String getString(String key) {
        if (this.condition.containsKey(key)) {
            return CastUtil.castString(this.condition.get(key));
        } else {
            return null;
        }
    }

    /**
     * @param key
     * @return
     */
    public Double getDouble(String key) {
        if (this.condition.containsKey(key)) {
            return CastUtil.castDouble(this.condition.get(key));
        } else {
            return null;
        }
    }

    /**
     * @param key
     * @return
     */
    public Long getLong(String key) {
        if (this.condition.containsKey(key)) {
            return CastUtil.castLong(this.condition.get(key));
        } else {
            return null;
        }
    }

    /**
     * @param key
     * @return
     */
    public Integer getInt(String key) {
        if (this.condition.containsKey(key)) {
            return CastUtil.castInt(this.condition.get(key));
        } else {
            return null;
        }
    }

    /**
     * @param key
     * @return
     */
    public Boolean getBoolean(String key) {
        if (this.condition.containsKey(key)) {
            return CastUtil.castBoolean(this.condition.get(key));
        } else {
            return null;
        }
    }

    /**
     * @return
     */
    public int getPageIndex() {
        if (this.offset == null || this.fetchSize == null) {
            return 0;
        }
        return this.offset / this.fetchSize;
    }
}
